﻿// 1.1.7.16. Write a program, which uses a binary search in a sorted array of integer numbers to find a certain element.

using System;
class BinarySearchInASortedArray
{
    static void Main()
    {
        int[] arr = new int[] { 3, 1, 5, 6, 1, 3, 1, 5, 6, 7, 1, 8 };
        Array.Sort(arr);
        //Console.WriteLine(Array.BinarySearch(arr, 7));    // 10
        //Console.WriteLine(arr[10]);   // 7
        int key = 5;
        int min = 0;
        int max = arr.GetLength(0);
        while (true)
        {
            int mid = min / 2 + max / 2;
            if (key == arr[mid])
            {
                Console.WriteLine("found");
                break;
            }
            else if (key < arr[mid])
            {
                max = mid;
            }
            else
            {
                min = mid;
            }
        }
    }
}
